﻿namespace IOLib
{
    using System;
    using System.Collections;
    using System.Drawing;
    using System.IO;
    using System.Reflection;
    using System.Windows.Forms;

    public class GlobalParams
    {
        //public static AnswerType ansType_ISO = AnswerType.Single;
        private static string applicationPath = null;
        //public static bool autoReCalc = true;
        public static Color buttonL = Color.Blue;
        //public static int clipper_ISO = 0;
        public static Color[] clr = new Color[] { SystemColors.WindowText, SystemColors.HighlightText, SystemColors.Window, SystemColors.Highlight, Color.Red, Color.FromArgb(127,Color.Black) };
        public static bool ctrlKeyPressed = false;
        public static string CurLang3Letter = "";
        public static string CurLangFull = "";
        public static string[] Descriptions;
        public static Control detector = new Control();
        public static string[] FatalErrors = new string[] { "Невозможно найти нужный языковой файл.\nПопробуйте переустановить програму.", "Ошибка загрузки скина." };
        public static int fontsize_ISO = 10;
        public static string FTitle = "SHS_SecondEdition";
        public static char fun = ((sep == '.') ? ',' : ';');
        public static int[] FunctionVisibility = new int[] { 
            0, -1, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 3, 
            3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 1, 0, 
            1, 1, 1, 1, 2, 1, 1, 1, 1, 0, 3, 3, 0, -1, 1, 1, 
            1, 1, 1, 2, 2, 0, 0, 2, 2, 0, 0, 1, 1, 1, -1, 0, 
            0, 0, 1, 1, 5, 0, 0, 5, -1, 5, 5, 1, -1, 0, 0, 0, 
            0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 
            1
         };
        public static string FVersion = "1.00";
        public static int gridSize = 9;
        //public static int integral_ISO = 100;
        public static int lang_ISO = 0;
        public static string[] LanguageErrors;
        public static string[] LanguageForm;
        public static string[,] Languages;
        //public static string[] MathBook;
        //public static int num_ISO = 4;
        //private static OSType os = OSType.NotDefined;
        //private static PlatformType platform = PlatformType.NotDefined;
        public static string pth;
        public static DateTime releaseDate = new DateTime(0x7d9, 9, 1);
        //public static int rootA_ISO = -20;
        //public static int rootB_ISO = 20;
        private static double screenDpi = 0.0;
        private static double screenResolutionFactor = 0.0;
        private static int scrollBarWidth = 0;
        public static char sep = Convert.ToChar(Convert.ToString((double) 0.5).TrimStart('0').TrimEnd('5'));
        public static bool shiftKeyPressed = false;
        public static bool smoothGraphics = false;
        //public static SysSettings sysset_ISO = SysSettings.None;
        public static Color titleC = SystemColors.ActiveCaption;
        public static float titleF = 7f;
        public static Color titleFC = Color.White;
        public static int titleH = 12;
        public static Bitmap[] toolBt;
        public static string[] ToolTip;
        public static Bitmap ttlCenter;
        public static Bitmap ttlLeft;
        public static Bitmap ttlRight;
        public static int Type_ISO = 1;
        /*public static string[] TypeEURO = new string[] { 
            "diff", "graph", "expand", "ln", "sh", "ch", "th", "cth", "sech", "cosech", "arsh", "arch", "arth", "arcth", "sign", "arcsin", 
            "arccos", "arctg", "arcctg", "sin", "cos", "tg", "ctg", "sec", "cosec", "lg", "log", "perc", "sqrt", "nthroot", "tr", "abs", 
            "det", "rank", "invert", "transpose", "arg", "vminor", "minor", "alg", "mat", "sys", "arcsec", "arccosec", "int", "a2t", "row", "col", 
            "el", "rows", "cols", "xy2pol", "pol2xy", "sum", "product", "Re", "Im", "polyroots", "exp", "max", "min", "submatrix", "eigenvals", "trunc", 
            "round", "mod", "identity", "length", "if", "random", "solve", "for", "switch", "while", "line", "matrix", "lim", "wfile", "rfile", "dfile", 
            "range", "Gamma", "numden", "stack", "diag", "augment", "eval", "linterp", "cinterp", "ainterp", "normi", "norm1", "norme", "sort", "rsort", "csort", 
            "reverse"
         };
        public static string[] TypeWORLD = new string[] { 
            "diff", "graph", "expand", "ln", "sinh", "cosh", "tanh", "coth", "sech", "csch", "asinh", "acosh", "atanh", "acoth", "sign", "asin", 
            "acos", "atan", "acot", "sin", "cos", "tan", "cot", "sec", "csc", "log10", "log", "perc", "sqrt", "nthroot", "tr", "abs", 
            "det", "rank", "invert", "transpose", "arg", "vminor", "minor", "alg", "mat", "sys", "arcsec", "arccosec", "int", "a2t", "row", "col", 
            "el", "rows", "cols", "xy2pol", "pol2xy", "sum", "product", "Re", "Im", "polyroots", "exp", "max", "min", "submatrix", "eigenvals", "trunc", 
            "round", "mod", "identity", "length", "if", "random", "solve", "for", "switch", "while", "line", "matrix", "lim", "wfile", "rfile", "dfile", 
            "range", "Gamma", "numden", "stack", "diag", "augment", "eval", "linterp", "cinterp", "ainterp", "normi", "norm1", "norme", "sort", "rsort", "csort", 
            "reverse"
         };*/

        public static void Compilator(string country, bool fullload)
        {
            Compilator(country, fullload, null);
        }

        public static void Compilator(string country, bool fullload, string path)
        {
            try
            {
                int num = -1;
                int num2 = 0;
                country = string.Concat(path ?? ApplicationPath, "lang", Path.DirectorySeparatorChar, "Text_", country.ToUpper(), ".lang");
                long length = new FileInfo(country).Length;
                FileStream stream = File.Open(country, FileMode.Open, FileAccess.Read);
                if (stream != null)
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        CurLang3Letter = reader.ReadLine();
                        CurLangFull = reader.ReadLine();
                        string str = reader.ReadLine();
                        char ch = str[10];
                        long num4 = Convert.ToInt64(str) + (Convert.ToInt64(ch.ToString()) * 0x5f5e100L);
                        bool flag = true;//((Convert.ToInt64(num4.ToString().Remove(0, 3)) / 10L) + length) == 0x63d76aL;
                        if (!fullload && flag)
                        {
                            return;
                        }
                        if (!flag)
                        {
                            throw new Exception();
                        }
                        //clipper_ISO = (num4.ToString()[2] == '8') ? 1 : 0;
                        Type_ISO = (num4.ToString()[1] == '7') ? 1 : 0;
                        while (reader.Peek() >= 0)
                        {
                            string str2 = reader.ReadLine();
                            if (str2.StartsWith("//"))
                            {
                                num++;
                                switch (num)
                                {
                                    case 0:
                                        LanguageForm = new string[Convert.ToInt16(str2.Remove(0, 2))];
                                        break;

                                    case 1:
                                        Descriptions = new string[Convert.ToInt16(str2.Remove(0, 2))];
                                        break;

                                    case 2:
                                        LanguageErrors = new string[Convert.ToInt16(str2.Remove(0, 2))];
                                        break;

                                    case 3:
                                    //    MathBook = new string[Convert.ToInt16(str2.Remove(0, 2))];
                                    //    break;

                                    //case 4:
                                        ToolTip = new string[Convert.ToInt16(str2.Remove(0, 2))];
                                        break;
                                }
                                num2 = 0;
                                continue;
                            }
                            if (str2 != "")
                            {
                                switch (num)
                                {
                                    case 0:
                                    {
                                        LanguageForm[num2++] = str2;
                                        continue;
                                    }
                                    case 1:
                                    {
                                        Descriptions[num2++] = str2;
                                        continue;
                                    }
                                    case 2:
                                    {
                                        LanguageErrors[num2++] = str2;
                                        continue;
                                    }
                                    case 3:
                                    {
                                    //    MathBook[num2++] = str2;
                                    //    continue;
                                    //}
                                    //case 4:
                                    //{
                                        ToolTip[num2++] = str2;
                                        continue;
                                    }
                                    case 5:
                                    {
                                        stream.Position = stream.Length - 1;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                stream.Close();
            }
            catch
            {
                throw new Exception(FatalErrors[0] + "\n" + country);
            }
        }

        /*public static void FunctionsChooser(string value)
        {
            if (value == "e")
            {
                FunctionName = TypeEURO;
                Type_ISO = 0;
            }
            else if (value == "w")
            {
                FunctionName = TypeWORLD;
                Type_ISO = 1;
            }
        }*/

        public static Bitmap GetImageFromResources(Assembly assembly, string resourceName)
        {
            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
            {
                return new Bitmap(stream);
            }
        }

        public static string IntToFileSize(double s)
        {
            int num = 0;
            while (((s / 1024.0) > 1.0) && (num < 4))
            {
                s /= 1024.0;
                num++;
            }
            string str = Convert.ToString(Math.Round(s, 2));
            switch (num)
            {
                case 0:
                    return (str + "B");

                case 1:
                    return (str + "K");

                case 2:
                    return (str + "M");

                case 3:
                    return (str + "G");

                case 4:
                    return (str + "T");
            }
            return str;
        }

        public static void LanguageChooser(string value)
        {
            lang_ISO = 0;
            if (value.Length != 3)
            {
                value = "rus";
            }
            for (int i = 0; i < Languages.GetLength(0); i++)
            {
                if (Languages[i, 0].ToLower() == value.ToLower())
                {
                    lang_ISO = i;
                    break;
                }
                if (Languages[i, 0].ToLower() == "rus")
                {
                    lang_ISO = i;
                }
            }
            Compilator(Languages[lang_ISO, 0], true);
        }

        public static void Loading()
        {
            Loading(ApplicationPath);
        }

        public static void Loading(string path)
        {
            try
            {
                FileInfo[] files = new DirectoryInfo(path + "lang" + Path.DirectorySeparatorChar).GetFiles("*.lang");
                Stack stack = new Stack();
                for (int i = 0; i < files.Length; i++)
                {
                    try
                    {
                        Compilator(files[i].Name.Substring(5, 3), false, path);
                        stack.Push(CurLang3Letter + "/" + CurLangFull);
                    }
                    catch
                    {
                    }
                }
                Languages = new string[stack.Count, 2];
                for (int j = stack.Count - 1; j >= 0; j--)
                {
                    string[] strArray = Convert.ToString((string) stack.Pop()).Split(new char[] { '/' });
                    Languages[j, 0] = strArray[0];
                    Languages[j, 1] = strArray[1];
                }
            }
            catch
            {
                throw new Exception(FatalErrors[0]);
            }
        }

        public static int ScreenFactor(int value)
        {
            return (int) (value * ScreenResolutionFactor);
        }

        public static string ApplicationPath
        {
            get
            {
                if (applicationPath == null)
                {
                    string fullyQualifiedName = Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
                    applicationPath = fullyQualifiedName.Substring(0, fullyQualifiedName.LastIndexOf(Path.DirectorySeparatorChar) + 1);
                }
                return applicationPath;
            }
        }

        /*public static OSType OS
        {
            get
            {
                if (os == OSType.NotDefined)
                {
                    if (System.Type.GetType("Mono.Runtime") != null)
                    {
                        os = OSType.Linux;
                    }
                    else
                    {
                        os = OSType.Windows;
                    }
                }
                return os;
            }
        }

        public static PlatformType Platform
        {
            get
            {
                if (platform == PlatformType.NotDefined)
                {
                    if (Environment.OSVersion.Platform == PlatformID.WinCE)
                    {
                        if (ScreenDpi == 131.0)
                        {
                            platform = PlatformType.Smartphone;
                        }
                        else if ((ScreenDpi == 128.0) || (ScreenDpi == 192.0))
                        {
                            platform = PlatformType.PPC;
                        }
                        else if (MessageBox.Show(FatalErrors[2], FTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                        {
                            platform = PlatformType.Smartphone;
                        }
                        else
                        {
                            platform = PlatformType.PPC;
                        }
                    }
                    else
                    {
                        platform = PlatformType.PC;
                    }
                }
                return platform;
            }
            set
            {
                platform = value;
            }
        }*/

        public static double ScreenDpi
        {
            get
            {
                if (screenDpi == 0.0)
                {
                    using (Graphics graphics = detector.CreateGraphics())
                    {
                        screenDpi = graphics.DpiY;
                    }
                }
                return screenDpi;
            }
        }

        public static double ScreenResolutionFactor
        {
            get
            {
                if (screenResolutionFactor == 0.0)
                {
                    screenResolutionFactor = ScreenDpi / 96.0;
                }
                return screenResolutionFactor;
            }
        }

        public static int ScrollBarWidth
        {
            get
            {
                if (scrollBarWidth == 0)
                {
                    //if (Platform == PlatformType.PC)
                    //{
                        scrollBarWidth = ScreenFactor(17);
                    //}
                    //else if (Platform == PlatformType.PPC)
                    //{
                    //    scrollBarWidth = ScreenFactor(13);
                    //}
                    //else if (Platform == PlatformType.Smartphone)
                    //{
                    //    scrollBarWidth = ScreenFactor(6);
                    //}
                }
                return scrollBarWidth;
            }
            set
            {
                scrollBarWidth = value;
            }
        }

        /*public enum OSType
        {
            NotDefined,
            Windows,
            Linux
        }

        public enum PlatformType
        {
            NotDefined,
            PC,
            PPC,
            Smartphone
        }*/
    }
}

